from astral.Net import TCPClient,errors

class MastermindTCPAdapter(TCPClient):
    def __init__(self,gameclient,host="127.0.0.1",port=1919):
        TCPClient.__init__(self)
        self.connect(host,port)
        self.gameclient = gameclient
    def send(self,data):
        self.send_data(data)
    def listen(self):
        try:
            data = self.check_for_data()
        except errors.SocketError:
            self.gameclient.handle_data({"action":"disconnected"})
            return
        if data:
            self.gameclient.handle_data(data)
    def close(self):
        self.quit()
        
from astral.PodSixNet.Connection import ConnectionListener

class PodSixTCPAdapter(ConnectionListener):
    def __init__(self,gameclient,host="127.0.0.1",port=1919):
        ConnectionListener.__init__(self)
        self.gameclient = gameclient
        self.Connect((host,port))
    def send(self,data):
        self.Send(data)
    def Network(self, data):
        self.gameclient.handle_data(data)
    def listen(self):
        self.Pump()
    def Pump(self):
        ConnectionListener.Pump(self)
        self.connection.Pump()
    def close(self):
        self.connection.close()